# coding=utf-8
import maya.mel as mel
import maya.cmds as cmds
import core as timelimit

class TrailingTail () :
	def Num_bin ( self ) :
		self.Aim_Obj = cmds.ls (sl = 1)
	# 创建
	def Create ( self ) :
		mel.eval ("AnimationSweep")
	# 获取属性信息
	def GetInfo ( self ) :
		ObjHistory = cmds.listHistory (self.Aim_Obj[0] , future = 1)
		MotionTrails = cmds.ls (type = "motionTrail")
		for self.MotionTrail in MotionTrails :
			if self.MotionTrail in ObjHistory :
				self.Expression_startName = self.MotionTrail + '_startTime'
				self.Expression_endName = self.MotionTrail + '_endTime'
				
	def SetExpression ( self) :
		print self.MotionTrail
		cmds.expression (n = self.Expression_startName , o = self.MotionTrail , s = self.MotionTrail + ".startTime=frame-" + str (self.Num) , ae = 1 , uc = all)
		cmds.expression (n = self.Expression_endName , o = self.MotionTrail , s = self.MotionTrail + ".endTime=frame" , ae = 1 , uc = all)
	
	def ReviseExpression ( self ) :
		cmds.expression (self.Expression_startName , e = 1 , s = self.MotionTrail + ".startTime=frame-" + str (self.Num) , o = self.MotionTrail , ae = 1 , uc = all)

	def chuangjian( self ):
		self.Num  = cmds.textField (self.name , q = 1 , text =1)
		self.Num_bin()
		self.Create()
		self.GetInfo()
		self.SetExpression()
		
	def xiugai( self ):
		self.Num = cmds.textField (self.name , q = 1 , text = 1)
		self.Num_bin()
		self.GetInfo()
		self.ReviseExpression()
		
	def TrailingTail_Win ( self  ) :
		if timelimit.ProductActivation().Win() == 0:
			return
		windowName = 'TrailingTail'
		if cmds.window (windowName , ex = True) :
			cmds.deleteUI (windowName)
		cmds.window (windowName,)
		cmds.columnLayout( adj = 1,columnAttach=('both', 5))
		cmds.text (label = u'创建：选择曲线，设置段数，然后点击创建',backgroundColor=[0,1,0])
		cmds.separator ()
		cmds.text (label = u'修改：选择要修改曲线，重新设置段数，然后点击修改\n创建过一次后只可以修改不可以重复创建',backgroundColor=[1,0.8,0.2])
		cmds.separator ()
		cmds.rowColumnLayout (numberOfColumns = 2  , columnWidth = [(1 , 50) ,(2 , 250)])
		
		cmds.text (label = u'段数')
		self.name = cmds.textField ()
		
		cmds.setParent ('..')
		
		cmds.rowColumnLayout( numberOfColumns=2,columnWidth=[(1, 150), (2, 150)] )
		cmds.button(label = u'创建',c = lambda *args : self.chuangjian())
		cmds.button(label = u'修改',c = lambda *args : self.xiugai())
		cmds.showWindow()

#TrailingTail () .TrailingTail_Win()